﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace VM.Wechat
{


    public class HttpClientRequest
    {
        private readonly HttpClient client;

        public HttpClientRequest()
        {
            client = new HttpClient();

        }

        public string GetToken(string requestUrl)
        {
            // 构造请求体对象
            var requestPayload = new
            {
                publicKey = "1A6053A9A79342A95426236F78E25F94",
                phone = "1",
                passWord = "AC1AAF604AFBE10FB12D82E3F4790FEA",
                clientId = "1",
                clientInfo = "1"
            };

            string jsonPayload = JsonConvert.SerializeObject(requestPayload);
            var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

            try
            {
                var response = client.PostAsync(requestUrl, httpContent);

                var responseContent = response.ToString();
                return responseContent;
            }
            catch (Exception ex)
            {
                // 处理异常情况
                Console.WriteLine($"请求失败: {ex.Message}");
                return null;
            }
        }

        public static string GetFunction(string Url)
        {
            string serviceAddress = Url;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }


    }
}